package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.JLabel;
import javax.swing.JPanel;

import model.Constants;

public class DisplayerPanel extends JPanel {

	private static final long serialVersionUID = 1814588748536831893L;

	private static DisplayerPanel displayerPanelInstance;	// Instance du singleton

	private LedPanel led1;									// Panneau pour la led de temps
	private LedPanel led2;									// Panneau pour la led de mesure
	private JLabel tempo;									// Label pour l'affichage du tempo

	public static DisplayerPanel displayerPanelInstance() {
		if(displayerPanelInstance == null) {
			displayerPanelInstance = new DisplayerPanel();
		}

		return displayerPanelInstance;
	}

	private DisplayerPanel() {
		setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
		led1 = new LedPanel();
		led2 = new LedPanel();
		tempo = new JLabel("" + Constants.DEFAULT_TEMPO);
		
		add(led1);
		add(led2);
		add(tempo);		
	}

	public void displayLed(int ledID) {
		switch(ledID) {
		case Constants.DEFAULT_TIME_LED : led1.turnOn(); break;
		case Constants.DEFAULT_BAR_LED : led2.turnOn(); break;
		default : break;
		}
	}

	public void undisplayLed(int ledID) {
		switch(ledID) {
		case Constants.DEFAULT_TIME_LED : led1.turnOff(); break;
		case Constants.DEFAULT_BAR_LED : led2.turnOff(); break;
		default : break;
		}
	}

	public void displayTempo(int tempo) {
		this.tempo.setText("" + tempo);
	}

	class LedPanel extends JPanel {

		private static final long serialVersionUID = -795941461119459695L;

		private boolean hidden;
		private Color color;
		
		public LedPanel() {
			setLayout(new FlowLayout());
			setPreferredSize(new Dimension(20, 20));
			color = new Color(240, 0, 0);
			turnOff();
		}

		public void turnOn() {
			hidden = false;
			repaint();
		}

		public void turnOff() {
			hidden = true;
			repaint();
		}

		public void paint(Graphics graphics) {
			Graphics2D graphics2D = (Graphics2D)graphics;
			super.paint(graphics2D);
			graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

			graphics2D.setColor(Color.BLACK);
			graphics2D.fillOval(0, 0, this.getWidth(), this.getHeight());

			if(!hidden) {
				graphics2D.setColor(color);
				graphics2D.fillOval(1, 1, this.getWidth() - 2, this.getHeight() - 2);
			}
		}
	}

}
